package wpmp.auth.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import wcommons.httputils.ResponseUtils;
import wcommons.httputils.quickbean.RespInfo;
import wpmp.auth.service.ResourceService;
import wpmp.utils.quickbean.NeedNotLoginPermission;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 11:47:52 AM Dec 5, 2013
 */
@Controller
@RequestMapping("/resource.do")
public class ResourceController {

	private static final Logger LOG = LoggerFactory.getLogger(ResourceController.class);

	private ResourceService service;

	@Autowired
	public void setService(ResourceService service) {
		this.service = service;
	}

	/**
	 * 检验权限
	 * 
	 * @param req
	 * @param resp
	 * @param securityId
	 * @param authUrl
	 */
	@RequestMapping(params = "action=check", method = { RequestMethod.GET, RequestMethod.POST })
	public void check(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "securityId", required = false) String securityId,
			@RequestParam(value = "authUrl", required = true) String authUrl) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to check Permission by [{}, {}]", req.getRemoteAddr(), securityId, authUrl);
		}

		final int result = service.check(securityId, authUrl);

		final RespInfo<Object> respInfo = new RespInfo<Object>(result);

		ResponseUtils.renderJson(resp, respInfo.toString());
	}

	/**
	 * 获取无需登录的权限列表
	 * 
	 * @param req
	 * @param resp
	 */
	@RequestMapping(params = "action=getNeedNotLoginPermission", method = { RequestMethod.GET, RequestMethod.POST })
	public void getNeedNotLoginPermission(HttpServletRequest req, HttpServletResponse resp) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to getNeedNotLoginPermission", req.getRemoteAddr());
		}

		final NeedNotLoginPermission result = service.getNeedNotLoginPermission();

		final RespInfo<NeedNotLoginPermission> respInfo = new RespInfo<NeedNotLoginPermission>(result);

		ResponseUtils.renderJson(resp, respInfo.toString());
	}

	/**
	 * 获取用户权限列表
	 * 
	 * @param req
	 * @param resp
	 * @param securityId
	 */
	@RequestMapping(params = "action=getUserPermission", method = { RequestMethod.GET, RequestMethod.POST })
	public void getUserPermission(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "securityId", required = true) String securityId) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to getUserPermission by [{}]", req.getRemoteAddr(), securityId);
		}

		ResponseUtils.renderJson(resp, service.getUserPermission(securityId).toString());
	}

	/**
	 * 获取用户权限视图
	 * 
	 * @param req
	 * @param resp
	 * @param securityId
	 */
	@RequestMapping(params = "action=getUserView", method = { RequestMethod.GET, RequestMethod.POST })
	public void getUserView(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "securityId", required = true) String securityId) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to getUserView by [{}]", req.getRemoteAddr(), securityId);
		}

		ResponseUtils.renderJson(resp, service.getUserView(securityId).toString());
	}
}
